<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="Bookmark" href="/favicon.ico">
    <link rel="Shortcut Icon" href="/favicon.ico"/>
    <!--[if lt IE 9]-->
    <script type="text/javascript" src="lib/html5shiv.js"></script>
    <script type="text/javascript" src="lib/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" href="lib/select2/select2.min.css">
    <link rel="stylesheet" type="text/css" href="lib/At.js-master/css/jquery.atwho.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css"/>
    <link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin"/>
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css"/>

    <title>新增用例</title>
</head>
<body>
<article class="page-container">
    <form method="post" class="form form-horizontal" id="form-admin-role-add">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;"><span class="c-red">*</span>服务：</label>
            <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <span class="select-box" style="width:80%;">
                    <select class="select" size="1" name="service" id="service" onchange="serviceChange()">
                        <!--<option value="" selected>请选择服务</option>-->
                    </select>
				</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;"><span class="c-red">*</span>接口：</label>
            <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <span class="select-box" style="width:80%;" id="ti-span">
                    <select class="select" prevalue="" size="1" name="ti" id="ti"
                            onchange="tiChange(getCasename(),this.getAttribute('prevalue'))">
                    </select>
				</span>
                <span id="caseNum" style="color:red;margin-left:5px;"></span>
            </div>
        </div>
        <!-- <div class="row cl">
             <label class="form-label col-xs-4 col-sm-3" style="width:20%;">测试类：</label>
             <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                 <span class="select-box" style="width:100%;" id="tClass-span">
                     <select class="select" size="1" name="tClass" id="tClass">
                     </select>
                 </span>
             </div>
         </div>-->
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;"><span class="c-red">*</span>用例名称：</label>
            <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <input type="text" class="input-text" value="" placeholder="用例名称" id="caseName" name="caseName"
                       style="width:80%;">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;">用例描述：</label>
            <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <input type="text" class="input-text" value="" placeholder="用例描述" id="remark" name="remark"
                       style="width:80%;">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;">运行环境：</label>
            <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <span class="select-box" style="width:80%;">
                    <select class="select" size="1" name="env" id="env">
                        <option value="0" selected>请选择环境</option>
                    </select>
				</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;">测试类：</label>
            <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <span class="select-box" style="width:80%;">
                    <select class="select" size="1" name="testclass" id="testclass">
                        <option value="" selected>请选择测试类</option>
                    </select>
				</span>
            </div>
        </div>
        <div class="row cl" id="headerDiv" name="headerDiv" style="display: none">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;">请求Headers：</label>
            <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <textarea id="headerText" name="headerText" cols="" rows="" class="textarea valid"
                          placeholder="" style="width:80%;"></textarea>
            </div>
            <div id="paramInput2" class="formControls col-xs-8 col-sm-9" hidden>
                <table id="paramTable1" style="width: 86%;">
                    <tr>
                        <td style="width: 30%;"><input type="text" class="input-text" value="" placeholder="headKey" id="headKey1" name="headDey1"></td>
                        <td style="width: 55%;">
                            <input type="text" class="input-text" value="" placeholder="headValue" id="headValue1" name="headValue1">
                            <a href="javascript:;" onclick="addInput()"><i class="Hui-iconfont">&#xe600;</i></a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;">请求参数：</label>
            <div id="request1" class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <textarea id="requestParam" name="requestParam" cols="" rows="" class="textarea valid"
                          placeholder="请输入请求参数..." style="width:80%;"></textarea>
            </div>
            <div id="paramInput1" class="formControls col-xs-8 col-sm-9" hidden>
                <table id="paramTable" style="width: 86%;">
                </table>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;"><span class="c-red">*</span>断言类型：</label>
            <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <span class="select-box" style="width:80%;">
                    <select class="select" size="1" name="assertType" id="assertType" onchange="assertTypeChage()">
                        <option value="" selected>请选择断言类型</option>
                    </select>
				</span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;">期望结果：</label>
            <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <textarea id="expected" name="expected" cols="" rows="" class="textarea valid"
                          placeholder="" style="width:80%;"></textarea>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;">后置处理：</label>
            <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <textarea id="cafter" name="cafter" cols="" rows="" class="textarea valid"
                          placeholder="此用例运行完成后处理的内容" style="width:80%;"></textarea>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3" style="width:20%;">设计人：</label>
            <div class="formControls col-xs-8 col-sm-9" style="width:80%;">
                <input type="text" class="input-text" value="" placeholder="" id="author" name="author"
                       style="width:80%;"
                       disabled="disabled">
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <button type="button" class="btn btn-success radius" id="admin-role-save" name="admin-role-save"
                        onclick="addTcase()"><i class="icon-ok"></i> 提交
                </button>
                <button type="button" class="btn btn-success radius" id="test" name="admin-role-save"
                        onclick="runTcase()"><i class="icon-ok"></i> 测试
                </button>
                <button type="button" class="btn btn-success radius" id="admin-role-close" name="admin-role-close"
                        onclick="layer_close()"><i class="icon-ok"></i> 关闭
                </button>
                <!--<button class="btn" data-dismiss="modal" aria-hidden="true" onclick="layer_close()">关闭</button>-->
            </div>
        </div>
    </form>
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="static/hry-auto/util.js"></script>
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/select2/select2.full.js"></script>
<script type="text/javascript" src="lib/select2/pinyin.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="lib/At.js-master/js/jquery.atwho.js"></script>
<script type="text/javascript" src="lib/At.js-master/js/jquery.caret.js"></script>

<script type="text/javascript">
    //loading
    var layerIndex;
    /*$(function () {
        $.ajaxSetup({
            layerIndex: -1,
            beforeSend: function () { //插件加载前
                layerIndex = layer.load(1, {shade: [1.01, '#fff']});
            },
            success:function(){
                layer.close(loading);
            },
            error: function () { //报错时执行
                layer.alert('显示异常，请刷新后重试', {
                    skin: 'layui-layer-molv'
                    , closeBtn: 0
                    , shift: 4 //动画类型
                });
            }
        });
    });*/

    function getCasename() {
        return $("#caseName").val();
    }

    $(document).ready(function () {
        /*debugger;*/
        var realName = $.cookie('realnameCookie');
        $("#author").val(realName);
        var serviceList = getServiceList();
        var tenvList = getTenvList();
        var assertTypeList = getAssertType();

        var selectedTi = null;
        var selectedTiId = null;

        var copyFlag = false;
        var copyTcase = null;
        if (getParameter("cId") != null && getParameter("cId") != "") {
            copyFlag = true;
            copyTcase = getTcaseById(getParameter("cId"));
        }
        var fromTiFlag = false;
        var fromTiSid = null;
        var fromTiIid = null;
        if (getParameter("fromTiSid") != null && getParameter("fromTiSid") != "" && getParameter("fromTiIid") != null && getParameter("fromTiIid") != "") {
            fromTiFlag = true;
            fromTiSid = getParameter("fromTiSid");
            fromTiIid = getParameter("fromTiIid");
        }

        //服务字段
        var sId = $.cookie('serviceid');
        var serviceHtml = "";
        for (var i = 0; i < serviceList.length; i++) {
            serviceHtml += "<option value=\"" + serviceList[i].id + "\">" + serviceList[i].servicekey + "</option>"
        }
        $("#service").append(serviceHtml);
        if (copyFlag) {
            $("#service").val(copyTcase.serviceid);
        } else if (fromTiFlag) {
            $("#service").val(fromTiSid);

        } else if (sId != null) {
            $("#service").val(sId);
        }
        var selectedServiceId = $("#service").val();

        //接口字段
        var tId = $.cookie('iid');
        var tiList = getTiList(selectedServiceId);
        var tiHtml = "";
        if (tiList.length == 0) {
            $("#ti").empty();
            tiHtml = "<option value=\"\" style='color:red'>该服务下无接口</option>";
            $("#ti").append(tiHtml);
        } else {
            for (var j = 0; j < tiList.length; j++) {
                tiHtml += "<option value=\"" + tiList[j].id + "\">" + tiList[j].iuri + "(" + tiList[j].remark + ")</option>";
            }
            $("#ti").append(tiHtml);
            $("#ti").select2();

            if (copyFlag) {//如果是复制用例,这里选中
                $("#ti").select2().val(copyTcase.iid).trigger("change");//注意,select2的选中与select并不相同
            } else if (fromTiFlag) {
                $("#ti").select2().val(fromTiIid).trigger("change");
            } else if (tId != null) {
                $("#ti").select2().val(tId).trigger("change");
            }
            selectedTiId = $("#ti").val();
            selectedTi = getTiById(selectedTiId);
        }


        //设置用例名称及prevalue
        if (copyFlag) {
            $("#caseName").val(copyTcase.casename);
        } else {
            $("#caseName").val(selectedTi.remark);
        }
        document.getElementById("ti").setAttribute("prevalue", $("#caseName").val());

        //设置用例描述
        if (copyFlag) {
            if (copyTcase.remark != null) {
                $("#remark").val(copyTcase.remark);
            }
        }

        //设置环境字段
        var tenvHtml = "";
        for (var i = 0; i < tenvList.length; i++) {
            tenvHtml += "<option value=\"" + tenvList[i].id + "\">" + tenvList[i].envkey + "</option>";
        }
        $("#env").append(tenvHtml);
        if (copyFlag) {
            $("#env").val(copyTcase.envid);
        }

        var num = selectedTi.casecount;
        $("#caseNum").text("已有" + num + "条用例");

        //设置测试类字段
        var selectedServiceKey = $("#service").find("option:selected").text();
        var testclassList = getTClassListByServiceKey(selectedServiceKey);
        var testclassHtml = "";
        for (var i = 0; i < testclassList.length; i++) {
            testclassHtml = testclassHtml + "<option value=\"" + testclassList[i] + "\">" + testclassList[i] + "</option>";
        }
        $("#testclass").append(testclassHtml);
        if (copyFlag && copyTcase.testclass != null && copyTcase.testclass != "") {
            $("#testclass").val(copyTcase.testclass);
        }

        //设置请求Headers
        var iheader = selectedTi.iheadersample;
        if (copyFlag && copyTcase.requestheader != null && copyTcase.requestheader != "") {
            $("#headerDiv").show();
            var headerJson = null;
            try {
                headerJson = JSON.stringify(JSON.parse(copyTcase.requestheader), null, 2);
            }
            catch (e) {
                headerJson = copyTcase.requestheader;
            }
            $("#headerText").val(headerJson);
        } else if (iheader != null && iheader != "") {
            $("#headerDiv").show();
            var headerJson;
            try {
                headerJson = JSON.stringify(JSON.parse(iheader), null, 2);
            }
            catch (e) {
                headerJson = iheader;
            }
            $("#headerText").val(headerJson);
        }

        //设置请求参数
        var iparamsample = selectedTi.iparamsample;
        var iparamtype = selectedTi.iparamtype;
        var sub = document.getElementById("admin-role-save");
        sub.removeAttribute("onclick");
        sub.setAttribute("onclick","addTcase("+iparamtype+")");
        var test = document.getElementById("test");
        test.removeAttribute("onclick");
        test.setAttribute("onclick","runTcase("+iparamtype+")");
        //调用requestParam的<触发事件
        //setRequestParameter();
        if (copyFlag) {//复制
            if (iparamtype == 1) {//接口参数类型为JSON,格式化后填充
                try {
                    $("#requestParam").val(JSON.stringify(JSON.parse(copyTcase.requestparam), null, 2));

                } catch (e) {
                    $("#requestParam").val(copyTcase.requestparam);
                }
            }else if(iparamtype == 2){//请求参数类型为map处理方式

                changeByParamType(copyTcase.requestparam);

            } else {
                alert("进来了！！！");
                //否则直接填充
                $("#requestParam").val(copyTcase.requestparam);
            }
        } else{//不是复制

            if (iparamtype == 1) {//请求参数类型为json
                try {
                    $("#requestParam").val(JSON.stringify(JSON.parse(iparamsample), null, 2));

                } catch (e) {
                    $("#requestParam").val(iparamsample);
                }
            } else if(iparamtype == 2){//请求参数类型为map处理方式

                changeByParamType(iparamsample);

            }else{
                //否则直接填充
                $("#requestParam").val(iparamsample);
            }


        }

        //设置断言类型
        var assertTypeHtml = "";
        for (var m = 0; m < assertTypeList.length; m++) {
            assertTypeHtml += "<option value=\"" + assertTypeList[m].id + "\">" + assertTypeList[m].value + "</option>";
        }
        $("#assertType").append(assertTypeHtml);
        if (copyFlag) {
            $("#assertType").val(copyTcase.asserttype);
        }

        //设置期望结果
        if (copyFlag) {
            if ($("#assertType").val() == 3 && copyTcase.expected != null && copyTcase.expected != "") {
                try {
                    $("#expected").val(JSON.stringify(JSON.parse(copyTcase.expected), null, 2));
                } catch (e) {
                    $("#expected").val(copyTcase.expected);
                }
            }
        }

        //设置后置处理
        if (copyFlag) {
            if (copyTcase.cafter != null) {
                $("#cafter").val(copyTcase.cafter);
            }
        }
        //调用requestParam的<触发事件
       setRequestParameter();
        layer.close(layerIndex);

    })
    ;

    function getParameter(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }

    //请求参数实例动态增加输入框处理
    function addInput(){

        var rows=document.getElementById("paramTable").rows.length;
        var row=rows+1;
        var html="<tr id='tr"+row+"'>\n" +
            "                        <td style='width: 30%'><input type=\"text\" class=\"input-text\" value=\"\" placeholder=\"key\" id=\"key"+row+"\" name=\"key"+row+"\"></td>\n" +
            "                        <td style='width: 55%;'>\n" +
            "                            <input type=\"text\" class=\"input-text\" value=\"\" placeholder=\"value\" id=\"value"+row+"\" name=\"value"+row+" \" autocomplete = \"off\">\n" +
            "                            <a href=\"javascript:;\" onclick=\"addInput()\"><i class=\"Hui-iconfont\">&#xe600;</i></a><a href='javascript:;' onclick='clearInput("+row+")'><i class=\"Hui-iconfont\">&#xe6a1;</i></a>\n" +
            "                        </td>\n" +
            "                    </tr>";

        $("#paramTable").append(html);

        //调用requestParam的<触发事件
        setRequestParameter();

    }


    //动态减少输入框处理
    function clearInput(row){
        document.getElementById("tr"+row).remove();

    }

    //接口改变处理
    function tiChange(cName, prevalue) {
        var tId = $("#ti").val();
        var t = getTiById(tId);
        var remark = t.remark;
        var paramType = t.iparamtype;
        var sub = document.getElementById("admin-role-save");
        sub.removeAttribute("onclick");
        sub.setAttribute("onclick","addTcase("+paramType+")");
        var test = document.getElementById("test");
        test.removeAttribute("onclick");
        test.setAttribute("onclick","runTcase("+paramType+")");

        if ($.trim(cName) == $.trim(prevalue) || $.trim(cName) == "") {//如果prevalue==cName,证明用户并没有编辑caseName,此时接口变更时,将会填充新的值
            $("#caseName").val(remark);
        }
        document.getElementById("ti").setAttribute("prevalue", remark);//设置新的自定义属性-prevalue,为下次change准备
        var num = t.casecount;
        $("#caseNum").text("已有" + num + "条用例");

        /**
         * 接口变更时,示例headers联动
         */
        var iHeader = t.iheadersample;
        if (iHeader != null && iHeader != "") {
            $("#headerDiv").show();
            try {
                var headerJson = JSON.stringify(JSON.parse(iHeader), null, 2)
                $("#headerText").val(headerJson);
            }
            catch (e) {
                $("#headerText").val(iHeader);
            }
        } else {
            $("#headerDiv").hide();
            $("#headerText").val("")
        }

        var iparamsample = t.iparamsample;
        var iparamtype = t.iparamtype;

        if(iparamtype == 1){//请求参数类型为json 处理逻辑
            document.getElementById("request1").style.display = "block";
            document.getElementById("paramInput1").style.display = "none";
            $("#paramTable").find("tr").remove();

            if (iparamsample != null && iparamsample.length > 0) {

                try {
                    var requestJson = JSON.stringify(JSON.parse(iparamsample), null, 2);
                    $("#requestParam").val(requestJson);
                } catch (e) {
                    $("#requestParam").val(iparamsample);
                }
            }else{
                $("#requestParam").val(iparamsample);
            }

        } else if(iparamtype == 2) {//请求参数类型为map处理逻辑

            changeByParamType(iparamsample);
        }else{//请求参数类型为text处理
            $("#requestParam").val(iparamsample);
        }
        //$("#requestParam").empty();
        //调用requestParam的<触发事件
        setRequestParameter();

    }

    //请求参数类型为map(2)展示方式
    function changeByParamType(iparamsample){
        document.getElementById("request1").style.display = "none";
        document.getElementById("paramInput1").style.display = "block";
        $("#paramTable").find("tr").remove();
        var inputHtml="";
        if(iparamsample == '{}'){
            inputHtml = "<tr id='tr1'>\n" +
                "                        <td style='width: 30%'><input type=\"text\" class=\"input-text\" value=\"\" placeholder=\"key\" id=\"key1\" name=\"key1\"></td>\n" +
                "                        <td style='width: 55%;'>\n" +
                "                            <input type=\"text\" class=\"input-text\" value=\"\" placeholder=\"value\" id=\"value1\" name=\"value1\" autocomplete = \"off\">\n" +
                "                            <a href=\"javascript:;\" onclick=\"0addInput()\"><i class=\"Hui-iconfont\">&#xe600;</i></a>\n" +
                "                        </td>\n" +
                "                    </tr>";
            $("#paramTable").append(inputHtml);

        }else{
            var iparamDemo=JSON.parse(iparamsample);
            var num=0;
            for(var key in iparamDemo) {
                var value = iparamDemo[key];
                num++;
                if (num == 1) {
                    inputHtml += "<tr id='tr" + num + "'>\n" +
                        "                        <td style='width: 30%'><input type=\"text\" class=\"input-text\" value=\"" + key + "\" placeholder=\"key\" id=\"key" + num + "\" name=\"key" + num + "\"></td>\n" +
                        "                        <td style='width: 55%;'>\n" +
                        "                            <input type=\"text\" class=\"input-text\" value=\"" + value + "\" placeholder=\"value\" id=\"value" + num + "\" name=\"value" + num + "\" autocomplete = \"off\">\n" +
                        "                            <a href=\"javascript:;\" onclick=\"addInput()\"><i class=\"Hui-iconfont\">&#xe600;</i></a><a href='javascript:;' onclick='clearInput(" + num + ")'></a>\n" +
                        "                        </td>\n" +
                        "                    </tr>";
                } else {
                    inputHtml += "<tr id='tr" + num + "'>\n" +
                        "                        <td style='width: 30%'><input type=\"text\" class=\"input-text\" value=\"" + key + "\" placeholder=\"key\" id=\"key" + num + "\" name=\"key" + num + "\"></td>\n" +
                        "                        <td style='width: 55%;'>\n" +
                        "                            <input type=\"text\" class=\"input-text\" value=\'" + value + "\' placeholder=\"value\" id=\"value" + num + "\" name=\"value" + num + "\" autocomplete = \"off\">\n" +
                        "                            <a href=\"javascript:;\" onclick=\"addInput()\"><i class=\"Hui-iconfont\">&#xe600;</i></a><a href='javascript:;' onclick='clearInput(" + num + ")'><i class=\"Hui-iconfont\">&#xe6a1;</i></a>\n" +
                        "                        </td>\n" +
                        "                    </tr>";
                }
            }
            $("#paramTable").append(inputHtml);
        }
        //调用requestParam的<触发事件
        //setRequestParameter();

    }


    //服务改变处理
    function serviceChange() {
        var serviceId = $("#service").val();
        var tiList = getTiList(serviceId);
        var tiHtml = "";
        if (tiList.length == 0) {
            $("#ti").empty();
            tiHtml = "<option value=\"\" style='color:red'>该服务下无接口</option>";
            $("#ti").append(tiHtml);
            $("#caseName").val("");
            $("#requestParam").val("");
            $("#headerDiv").hide();
            $("#headerText").val("");
        } else {
            for (var j = 0; j < tiList.length; j++) {
                var row = tiList[j];
                var rowHtml = "<option value=\"" + row.id + "\">" + row.iuri + "(" + row.remark + ")</option>";
                tiHtml = tiHtml + rowHtml;
            }
            //$("#ti").find("option:selected").text("");
            $("#ti").empty();
            $("#ti").append(tiHtml);
            $("#ti").select2();
            tiChange();

        }
        //服务改变时,测试类也需要联动
        var selectedServiceKey = $("#service").find("option:selected").text();
        var testclassList = getTClassListByServiceKey(selectedServiceKey);
        var testclassHtml = "<option value=\"\" selected>请选择测试类</option>";
        for (var i = 0; i < testclassList.length; i++) {
            testclassHtml = testclassHtml + "<option value=\"" + testclassList[i] + "\">" + testclassList[i] + "</option>";
        }
        $("#testclass").empty();
        $("#testclass").append(testclassHtml);

        var serviceKey = $("#service option:selected").text();
        var tClassList = getTClassListByServiceKey(serviceKey);
        $("#tClass").empty();
        var tClassHtml = "";
        for (var m = 0; m < tClassList.length; m++) {
            var tClass = tClassList[m];
            var tClassOption = "<option>" + tClass + "</option>";
            tClassHtml = tClassHtml + tClassOption;

        }
        $("#tClass").append(tClassHtml);

        layer.close(layerIndex);

    }

    //断言类型改变处理
    function assertTypeChage() {
        var assertType = $("#assertType").val();
        var assertTypeList = getAssertType();
        var placeholder = "";
        for (var i = 0; i < assertTypeList.length; i++) {
            var row = assertTypeList[i];
            if (row.id == assertType) {
                placeholder = row.desc;
                $("#expected").prop("placeholder", placeholder);
                break;
            } else {
                $("#expected").prop("placeholder", "");
            }
        }


    }

    //添加用例
    function addTcase(paramType) {
        var caseName = $("#caseName").val();
        var remark = $("#remark").val();
        var env = $("#env").val();
        var service = $("#service").val();
        var ti = $("#ti").val();
        var requestHeader = $("#headerText").val();

        var requestParam;
        if(paramType == 2){

            var table=document.getElementById("paramTable");
            var rows=table.rows;
            var json;

            json = "{";
            for(i=0;i<rows.length;i++){
                var rowList=rows[i].getElementsByTagName("input");
                if(rowList[0].value.length>0 || rowList[1].value.length>0){
                    var key=rowList[0].value;
                    var value=rowList[1].value;
                    //某个key的值还是json对象格式，不用加双引号
                    if(isJsonString(value)){

                        json+='\"'+key+'\"'+':'+JSON.stringify(value)+',';

                    }else{

                        json+='\"'+key+'\"'+':\"'+value+'\",';
                    }
                }

            }
            if(json.length>1){
                json = json.substring(0, json.length-1);
            }

            json+="}";
            requestParam=json;

        }else{
            requestParam = $("#requestParam").val();
        }

        var assertType = $("#assertType").val();
        var expected = $("#expected").val();
        var cafter = $("#cafter").val();
        var author = $("#author").val();
        var testclass = $("#testclass").val();

        if (caseName == null || caseName == "") {
            layer.alert("用例名称不能为空！");
            return;
        } else if (ti == null || ti == "") {
            layer.alert("请选择接口！");
            return;
        } else if (assertType == null || assertType == "") {
            layer.alert("请选择断言类型！");
            return;
        } else {
            $.ajax({
                type: "post",
                url: "/tcase/insertOne",
                data: {
                    casename: caseName,
                    remark: remark,
                    serviceid: service,
                    iid: ti,
                    envid: env,
                    requestheader: requestHeader,
                    requestparam: requestParam,
                    asserttype: assertType,
                    expected: expected,
                    cafter: cafter,
                    author: author,
                    testclass: testclass

                },
                dataType: "json",
                success: function (data) {

                    var status = data.status;
                    var msg = data.msg;
                    if (status == 0) {
                        $.cookie("serviceid", service, {expires: 3650});
                        $.cookie("iid", ti, {expires: 3650});
                        //parent.window.location.reload();
                        window.parent.pageSkip(1);
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);

                    } else {
                        layer.close(layerIndex);
                        layer.alert(msg, {
                            icon: 0,
                            skin: 'layer-ext-moon'
                        });
                    }

                },
                fail: function (data) {
                    layer.close(layerIndex);
                    layer.alert(JSON.stringify(data), {
                        icon: 0,
                        skin: 'layer-ext-moon'
                    });
                },
                error: function (xhr) {
                    layer.close(layerIndex);
                    layer.alert('Error' + JSON.stringify(xhr), {
                        icon: 2,
                        skin: 'layer-ext-moon'
                    });
                }
            });
        }

    }

    //运行用例
    function runTcase(paramType) {
        var caseName = $("#caseName").val();
        var remark = $("#remark").val();
        var env = $("#env").val();
        var service = $("#service").val();
        var ti = $("#ti").val();
        var requestHeader = $("#headerText").val();
        var requestParam;
        if(paramType == 2){

            var table=document.getElementById("paramTable");
            var rows=table.rows;
            var json;

            json = "{";
            for(i=0;i<rows.length;i++){
                var rowList=rows[i].getElementsByTagName("input");
                if(rowList[0].value.length>0 || rowList[1].value.length>0){
                    var key=rowList[0].value;
                    var value=rowList[1].value;
                    //某个key的值还是json对象格式，不用加双引号
                    if(isJsonString(value)){

                        json+='\"'+key+'\"'+':'+JSON.stringify(value)+',';

                    }else{

                        json+='\"'+key+'\"'+':\"'+value+'\",';
                    }
                }

            }
            if(json.length>1){
                json = json.substring(0, json.length-1);
            }

            json+="}";
            requestParam=json;

        }else{
            requestParam = $("#requestParam").val();
        }

        var assertType = $("#assertType").val();
        var expected = $("#expected").val();
        var cafter = $("#cafter").val();
        var testClass = $("#testclass").val();
        var userId = $.cookie('uidCookie');

        if (ti == null || ti == "") {
            layer.alert("请选择接口！");
            return;
        } else if (assertType == null || assertType == "") {
            layer.alert("请选择断言类型！");
            return;
        } else {
            $.ajax({
                type: "post",
                url: "/tcase/runCase",
                data: {
                    casename: caseName,
                    remark: remark,
                    iid: ti,
                    envid: env,
                    serviceid: service,
                    requestheader: requestHeader,
                    requestparam: requestParam,
                    testclass: testClass,
                    asserttype: assertType,
                    expected: expected,
                    cafter: cafter,
                    userId: userId
                },
                dataType: "json",
                success: function (data) {
                    var url = data.data;
                    var status = data.status;
                    var msg = data.msg;
                    if (status == 0) {
                        layer.close(layerIndex);
                        var tiName = $("#ti").find("option:selected").text();
                        url = encodeURI("../report-loading.html?reportname=" + url + "&customname=" + tiName);
                        window.open(url);
                        /*var domain = "http://" + window.location.host;
                        var myPopup = window.open("test-result.html");//.postMessage(list,domain);
                        //周期性的发送消息
                        setTimeout(function () {
                            myPopup.postMessage(list, domain);
                        }, 1000);
                        /!* parent.window.location.reload();
                         var index = parent.layer.getFrameIndex(window.name);
                         parent.layer.close(index);*!/*/
                    } else {
                        layer.close(layerIndex);
                        layer.alert(msg, {
                            icon: 0,
                            skin: 'layer-ext-moon'
                        });
                    }

                },
                fail: function (data) {
                    layer.close(layerIndex);
                    layer.alert(JSON.stringify(data), {
                        icon: 0,
                        skin: 'layer-ext-moon'
                    });
                },
                error: function (xhr) {
                    layer.close(layerIndex);
                    layer.alert('Error' + JSON.stringify(xhr), {
                        icon: 2,
                        skin: 'layer-ext-moon'
                    });
                }
            });
        }


    }


</script>

</body>
</html>